<?php

/**
 * Pegar as informações de latitude, longitude e zoom de um endereço usando a API do Google Maps
 */
class GMaps {

    private $mapsHost = 'maps.google.com';
    private $mapsKey = GOOGLE_API_KEY; // Sua Google Maps API Key definida em global.php

    function loadUrl($url) {
        if (function_exists('curl_init')) {
            $cURL = curl_init($url);
            curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($cURL);
            curl_close($cURL);
        } else {
            $result = file_get_contents($url);
        }

        if (!$result) {
            return false;
        } else {
            return $result;
        }
    }

    function geoLocal($address) {
        $url = 'http://' . $this->mapsHost . '/maps/geo?output=csv&key=' . $this->mapsKey . '&q=' . urlencode($address);

        $data = $this->loadUrl($url);
        list($status, $zoom, $lat, $lon) = explode(',', $data);
        if ($status != 200) {
            return false;
        }
        return array('lat' => $lat, 'lon' => $lon, 'zoom' => $zoom, 'address' => $address);
    }

}

?>
